package com.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.service.edu.pojo.Teacher;
import com.service.edu.mapper.TeacherMapper;
import com.service.edu.pojo.vo.TeacherQuery;
import com.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.utils.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-09-18
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {



    @Override
    public ResultVo page(Integer pageNum, Integer rows, TeacherQuery teacherQuery) {
        // 构建查询条件
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        //声明分页对象 pageNum：当前页数   rows:每页显示条数
        Page<Teacher> page = new Page<>(pageNum,rows);
        //判断查询条件是否为空
        if (teacherQuery==null){
            //如果为空则分页  不做查询
            this.page(page, null);
        }else{
            //判断name是否为空
            if (!StringUtils.isEmpty(teacherQuery.getName())){
                //如果不为空  则模糊查询name
                queryWrapper.like("name",teacherQuery.getName());
            }
            if (!StringUtils.isEmpty(teacherQuery.getLevel())){
                queryWrapper.eq("level",teacherQuery.getLevel());
            }
            if (!StringUtils.isEmpty(teacherQuery.getBegin())){
                queryWrapper.ge("gmt_create",teacherQuery.getBegin());
            }
            if (!StringUtils.isEmpty(teacherQuery.getEnd())){
                queryWrapper.le("gmt_create",teacherQuery.getEnd());
            }
            this.page(page, queryWrapper);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("pages",page.getPages());
        map.put("pageNum",page.getCurrent());
        map.put("items",page.getRecords());
        return ResultVo.ok().data(map);
    }
}
